<?php

namespace Lib;

class ZList {
	public $limit = 30;
	public $page = 0;
	public $posts = array();
	public $total_posts = 0;
	public $url = '';
	public $table = '';
	public $select = '*';
	public $vars = array();
	public $pagination_configs = array();
	public $DB = null;
	public $wheres = array();
	public $fulltexts = array();
	public $order_by = '';

	public function __construct($table = null, $configs = array()) {
		if ($table !== null) {
			$this->table = $table;
		}

		if (is_array($configs) && count($configs)) {
			foreach ($configs as $name => $value) {
				$this->{$name} = $value;
			}
		}
	}

	public static function forge($table, $configs = array()) {
		return new ZList($table, $configs);
	}

	public function set_vars($vars = array()) {
		$this->vars = array_merge($this->vars, $vars);
		return $this;
	}

	public function fulltext() {
		$args = func_get_args();
		if (count($args) === 1) {
			if (is_array($args)) {
				foreach ($args[0] as $arg) {
					$this->fulltexts[] = $arg;
				}
			}
		}

		if (count($args) === 2) {
			$this->fulltexts[] = array(
				$args[0],
				$args[1]
			);
		}

		return $this;
	}

	public function where() {
		$this->wheres[] = func_get_args();
		return $this;
	}

	public function order_by($field = '', $by = 'asc') {
		if ($field !== '') {
			$this->order_by[] = array(
				$field,
				$by
			);
		}

		return $this;
	}

	public function get() {
		if (isset($this->count_object)) {
			$query = $this->select_object;
		}
		else {
			$query = \DB::select(\DB::expr('COUNT(*) AS `total_posts`'))->from($this->table);
		}
		if (count($this->wheres) > 0) {
			$query = $query->where($this->wheres);
		}
		if (count($this->fulltexts) > 0) {
			$query = $query->or_where_open();
			foreach ($this->fulltexts as $fulltext) {
				$query = $query->or_where($fulltext[0], 'LIKE', "%" . $fulltext[1] . "%");
			}
			$query = $query->or_where_close();
		}
		$this->total_posts = $query->execute()->get('total_posts');

		if (isset($this->select_object)) {
			$query = $this->select_object;
		}
		else {
			$query = \DB::select($this->select)->from($this->table);
		}
		if (count($this->wheres) > 0) {
			$query = $query->where($this->wheres);
		}
		if (count($this->fulltexts) > 0) {
			$query = $query->or_where_open();
			foreach ($this->fulltexts as $fulltext) {
				$query = $query->or_where($fulltext[0], 'LIKE', "%" . $fulltext[1] . "%");
			}
			$query = $query->or_where_close();
		}
		$this->posts = $query->limit($this->limit)->offset($this->page * $this->limit)->execute();
		$vars = '';
		if (count($this->vars) > 0) {
			$vars = '?' . http_build_query($this->vars);
		}
		$pagination = \Pagination::forge($this->table . '_page', array(
			'pagination_url' => \Uri::create($this->url !== '' ? $this->url : '') . $vars,
			'total_items' => $this->total_posts,
			'per_page' => $this->limit,
			'current_page' => $this->page,
			'num_links' => 3,
			'show_first' => true,
			'show_last' => true,
			'uri_segment' => 'p'
		));

		return (object) array(
			'total_posts' => $this->total_posts,
			'posts' => json_decode(json_encode($this->posts->as_array())),
			'pagination' => $pagination
		);
	}

}
